package com.wx.sys.control;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.annotation.FormModel;
import com.wx.base.entity.PageInfo;
import com.wx.sys.entity.Wx_sys_user;
import com.wx.sys.service.Wx_sys_user_Service;
import com.wx.sys.vo.Wx_sys_user_VO;

/**
 * 后台管理员控制类
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/ll_sys_user")
public class Wx_sys_user_Controller {
	private static final Log log = LogFactory.getLog(Wx_sys_user_Controller.class);

	@Resource(name = "Wx_sys_user_Service")
	private Wx_sys_user_Service wx_sys_user_Service;

	/**
	 * 去分页
	 */
	@RequestMapping(value = "toList")
	public String toList(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		return "modules/sys/ll_sys_userList";
	}

	/**
	 * 分页
	 */
	@RequiresPermissions("sys:ll_sys_user:view")
	@RequestMapping(value = "list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request, HttpServletResponse response, Model model,
			@FormModel("ll_sys_user_vo") Wx_sys_user_VO wx_sys_user_vo) throws Exception {
		if (wx_sys_user_vo == null) {
			wx_sys_user_vo = new Wx_sys_user_VO();
		}
		if (request.getParameter("page") != null) {
			wx_sys_user_vo.setPage(Integer.parseInt(request.getParameter("page")));
		}
		if (request.getParameter("rows") != null) {
			wx_sys_user_vo.setRows(Integer.parseInt(request.getParameter("rows")));
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			PageInfo pageInfo = wx_sys_user_Service.selectPage(wx_sys_user_vo);
			jsonMap.put("total", pageInfo.getTotalCount());
			jsonMap.put("pages", pageInfo.getTotalPageCount());
			jsonMap.put("rows", pageInfo.getData());
		} catch (Exception e) {
			log.error("系统异常", e);
		}
		return jsonMap;
	}

	/**
	 * 去新增
	 */
	@RequiresPermissions("sys:ll_sys_user:add")
	@RequestMapping(value = "toAdd")
	public String toAdd(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		return "modules/sys/ll_sys_userAdd";
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("sys:ll_sys_user:add")
	@RequestMapping(value = "add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request, HttpServletResponse response, Model model,
			@FormModel("ll_sys_user") Wx_sys_user wx_sys_user) throws Exception {
		wx_sys_user.setCreatetime(new Timestamp(System.currentTimeMillis()));
		wx_sys_user.setUserpwd("");
		wx_sys_user.setStatus(Integer.parseInt("1"));// 默认用户状态:1-正常;2-禁用
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			boolean result = wx_sys_user_Service.add(wx_sys_user);
			if (result) {
				jsonMap.put("success", true);
				jsonMap.put("msg", "操作成功");
			} else {
				jsonMap.put("success", false);
				jsonMap.put("msg", "操作失败");
			}
		} catch (Exception e) {
			log.error("系统异常", e);
			jsonMap.put("success", false);
			jsonMap.put("msg", "操作失败");
		}
		return jsonMap;
	}

	/**
	 * 去修改
	 */
	@RequestMapping(value = "toUpdate")
	public String toUpdate(HttpServletRequest request, HttpServletResponse response, Model model, java.lang.Long id) throws Exception {
		model.addAttribute("item", wx_sys_user_Service.get(id));
		return "modules/sys/ll_sys_userUpdate";
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request, HttpServletResponse response, Model model,
			@FormModel("ll_sys_user") Wx_sys_user wx_sys_user) throws Exception {
		boolean isCheck = false;
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			Wx_sys_user_VO wx_sys_user_VO = new Wx_sys_user_VO();
			wx_sys_user_VO.setUsername(wx_sys_user.getUsername());
			List<Wx_sys_user> list = wx_sys_user_Service.selectAll(wx_sys_user_VO);
			if (list == null && list.size() == 0) {
				isCheck = true;
			} else {
				boolean t = false;
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getUsername().equals(wx_sys_user.getUsername())
							&& !list.get(i).getId().toString().equals(wx_sys_user.getId().toString())) {
						t = true;
					}
				}
				if (t) {
					jsonMap.put("success", false);
					jsonMap.put("msg", "已经有了该用户！");
				} else {
					isCheck = true;
				}
			}
			if (isCheck) {
				boolean result = wx_sys_user_Service.update(wx_sys_user);
				if (result) {
					jsonMap.put("success", true);
					jsonMap.put("msg", "操作成功");
				} else {
					jsonMap.put("success", false);
					jsonMap.put("msg", "操作失败");
				}
			}
		} catch (Exception e) {
			log.error("系统异常", e);
			jsonMap.put("success", false);
			jsonMap.put("msg", "操作失败");
		}
		return jsonMap;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request, HttpServletResponse response, Model model, java.lang.Long id) throws Exception {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			boolean result = wx_sys_user_Service.delete(id);
			if (result) {
				jsonMap.put("success", true);
				jsonMap.put("msg", "操作成功");
			} else {
				jsonMap.put("success", false);
				jsonMap.put("msg", "操作失败");
			}
		} catch (Exception e) {
			log.error("系统异常", e);
			jsonMap.put("success", false);
			jsonMap.put("msg", "操作失败");
		}
		return jsonMap;
	}

	/**
	 * 查看详情
	 */
	@RequestMapping(value = "info")
	public String info(HttpServletRequest request, HttpServletResponse response, Model model, java.lang.Long id) throws Exception {
		model.addAttribute("item", wx_sys_user_Service.get(id));
		return "modules/sys/ll_sys_userInfo";
	}
}
